package hauwei.exam416;

import java.util.*;

public class IP {
    static List<IPRange> result=new ArrayList<>();
    static long resultIpLong=Long.MAX_VALUE;
    static class IPRange{
        String start;
        String end;
        long startValue;
        long endValue;
        long length;
        public IPRange(String start,String end){
            this.start=start;
            this.end=end;
            this.startValue=ipToLong(start);
            this.endValue=ipToLong(end);
            this.length=endValue-startValue+1;
        }
        private long ipToLong(String ip){
            String[]ipParts=ip.split("\\.");
            long result=0;
            for(int i=0;i<4;i++){
                result=result<< 8 | Integer.parseInt(ipParts[i]);
            }
            return result;
        }

        @Override
        public String toString() {
            return start+ " " +end;
        }
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        sc.nextLine();

        List<IPRange>ranges=new ArrayList<>();
        for(int i=0;i<n;i++){
            String[]input=sc.nextLine().split(" ");
            ranges.add(new IPRange(input[0],input[1]));
        }

        Collections.sort(ranges, Comparator.comparingLong(r->r.endValue));

        dfs(0,new ArrayList<>(),ranges,0);

        for (IPRange range:result){
            System.out.println(range);
        }
    }
    static void dfs(int index,List<IPRange>reIps,List<IPRange>ranges,long ipCount){
        if (ipCount>resultIpLong){
            return;
        }
        if(index==ranges.size()){
            if(result.size()<reIps.size()){
                result=new ArrayList<>(reIps);
                resultIpLong=ipCount;
                return;
            }else if(result.size()==reIps.size()){
                if(ipCount<resultIpLong){
                    result=new ArrayList<>(reIps);
                    resultIpLong=ipCount;
                    return;
                }else{
                    for(int i=0;i<reIps.size();i++){
                        if(reIps.get(i).startValue<ranges.get(i).startValue){
                            result=new ArrayList<>(reIps);
                            return;
                        }else if(reIps.get(i).startValue>ranges.get(i).startValue){
                            result=new ArrayList<>(reIps);
                            return;
                        }
                    }
                }
            }
            return;
        }
        if(reIps.isEmpty()){
            reIps.add(ranges.get(index));
            dfs(index+1,new ArrayList<>(reIps),ranges,ipCount+ranges.get(index).length);
        }else{
            IPRange lastIp=reIps.get(reIps.size()-1);
            if(lastIp.endValue<=ranges.get(index).startValue){ //如果这个可以选
                dfs(index+1,new ArrayList<>(reIps),ranges,ipCount); //不选

                //选
                reIps.add(ranges.get(index));
                dfs(index+1,new ArrayList<>(reIps),ranges,ipCount+ranges.get(index).length);
            }else{//如果这个不可以选
                dfs(index+1,new ArrayList<>(reIps),ranges,ipCount);
            }
        }
    }
}
